/*
 * 文件名： AcceptAddressChangeActivity.java
 * 描    述：修改联系人信息
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppContext;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.CustomAutoCompleteTextView;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.miguo.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class AcceptAddressChangeActivity extends BaseActivity implements OnClickListener {
    private CustomAutoCompleteTextView mEtName, mEtPhone, mEtYouBian, mEtScreet;
    private TextView mEtArea;
    private Button mButton;
    private String province, city, town;

    private final int REQUEST_CODE = 1;

    private BaseHeadView mBaseHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accept_address_change);
        findViewById();
        initView();
        initListener();
    }

    private void findViewById() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle("修改收货信息");
        // 姓名
        mEtName = (CustomAutoCompleteTextView) findViewById(R.id.acceptAddress_name);
        // 电话
        mEtPhone = (CustomAutoCompleteTextView) findViewById(R.id.acceptAddress_phone);
        // 邮编
        mEtYouBian = (CustomAutoCompleteTextView) findViewById(R.id.acceptAddress_youBian);
        // 省市区
        mEtArea = (TextView) findViewById(R.id.acceptAddress_area);
        // 街道地址
        mEtScreet = (CustomAutoCompleteTextView) findViewById(R.id.acceptAddress_street);
        // 修改
        mButton = (Button) findViewById(R.id.acceptAddress_btn);
    }

    private void initView() {
        mEtName.setText(AppContext.UserInfo().getTruename());
        mEtPhone.setText(AppContext.UserInfo().getTelePhone());
        mEtYouBian.setText(AppContext.UserInfo().getPostcode());
        mEtArea.setText(AppContext.UserInfo().getArea());
        mEtScreet.setText(AppContext.UserInfo().getAddress());
        province = AppContext.UserInfo().getProvince();
        city = AppContext.UserInfo().getCity();
        town = AppContext.UserInfo().getTown();

        // 设置软键盘不自动打开
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//        mEtName.requestFocus();
//        InputMethodManager imm = (InputMethodManager) mEtName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.showSoftInput(mEtName, 0);
    }

    private void initListener() {
        mEtArea.setOnClickListener(this);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.acceptAddress_area:
                startActivityForResult(new Intent(mActivity, ChoosePlaceActivity.class), REQUEST_CODE);
                break;
            case R.id.acceptAddress_btn:
                if (verification()) {
                    saveAddress();
                }
                break;
        }
    }

    private void saveAddress() {

        AjaxCallBack callBack = new AjaxCallBack(mActivity, "保存中,请稍等...") {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);
                if (json.optInt("status") == 200) {
                    AppContext.UserInfo().setTruename(mEtName.getText().toString());
                    AppContext.UserInfo().setTelePhone(mEtPhone.getText().toString());
                    AppContext.UserInfo().setProvince(province);
                    AppContext.UserInfo().setCity(city);
                    AppContext.UserInfo().setTown(town);
                    String area = String.format("%s %s %s", province, city,
                            town);
                    AppContext.UserInfo().setArea(area);
                    AppContext.UserInfo().setAddress(mEtScreet.getText().toString());
                    AppContext.UserInfo().setPostcode(mEtYouBian.getText().toString());
                    setResult(RESULT_OK);
                    mActivity.finish();
                }
                Toast.show(mActivity, json.optString("info"));
            }
        };

        Map<String, String> param = new HashMap<String, String>();
        param.put("truename", mEtName.getText().toString());
        param.put("telephone", mEtPhone.getText().toString());
        param.put("province", province);
        param.put("city", city);
        param.put("town", town);
        param.put("address", mEtScreet.getText().toString());
        param.put("postcode", mEtYouBian.getText().toString());
        param.put("token", AppContext.UserInfo().getToken());
        HttpClient.postEncryptData(Url.toRoute(Url.UserCentre.EditAddress, 1), param,
                callBack);

    }

    private boolean verification() {
        if (StringUtils.isEmpty(mEtName.getText().toString())) {
            mEtName.setShakeAnimation();
            Toast.show(mActivity, "请输入收货人姓名");
            return false;
        }
        if (!StringUtils.isPhoneNumber(mEtPhone.getText().toString())) {
            mEtPhone.setShakeAnimation();
            Toast.show(mActivity, "手机号码格式错误");
            return false;
        }
        if (StringUtils.isEmpty(mEtYouBian.getText().toString())) {
            mEtYouBian.setShakeAnimation();
            Toast.show(mActivity, "请填写邮政编码");
            return false;
        }
        if (StringUtils.isEmpty(mEtArea.getText().toString())) {
            Toast.show(mActivity, "请选择省市");
            return false;
        }
        if (StringUtils.isEmpty(mEtScreet.getText().toString())) {
            mEtScreet.setShakeAnimation();
            Toast.show(mActivity, "请填写详细地址");
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent intent) {
        if (arg0 == REQUEST_CODE && arg1 == RESULT_OK) {
            province = StringUtils.isEmpty(intent.getStringExtra("province")) ? ""
                    : intent.getStringExtra("province");
            city = StringUtils.isEmpty(intent.getStringExtra("city")) ? ""
                    : intent.getStringExtra("city");
            town = StringUtils.isEmpty(intent.getStringExtra("town")) ? ""
                    : intent.getStringExtra("town");

            mEtArea.setText(String.format("%s  %s  %s", province, city, town));
        }
        super.onActivityResult(arg0, arg1, intent);
    }
}
